package com.jcgyl.export.service.Impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jcgyl.export.dao.TborderheadDao;
import com.jcgyl.export.dao.TborderlistDao;
import com.jcgyl.export.dao.TbreceiptDao;
import com.jcgyl.export.entity.Tborderhead;
import com.jcgyl.export.entity.Tborderlist;
import com.jcgyl.export.entity.Tbreceipt;
import com.jcgyl.export.mapper.TborderheadMapper;
import com.jcgyl.export.mapper.TborderlistMapper;
import com.jcgyl.export.service.OrderService;
import com.jcgyl.export.util.entity.Result;

@Service
public class OrderServiceImpl implements OrderService {

	@Autowired
	private TborderheadDao tborderheadDao;
	@Autowired
	private TborderlistDao tborderlistDao;
	@Autowired
	private TborderheadMapper orderheadMapper;
	@Autowired
	private TborderlistMapper orderlistMapper;
	@Autowired
	private TbreceiptDao tbreceiptDao;

	@Override
	public Map<String, Object> findObject(String orderid, String batchnumber, String expressnumber, String starttime,
			String endtime, int pageSize, int pageCount) {
		int startIndex = (pageCount - 1) * pageSize;
		List<Tborderhead> list = tborderheadDao.findObject(orderid, batchnumber, expressnumber, starttime, endtime,
				startIndex, pageSize);
		int rowCount = tborderheadDao.getRowCount(orderid, batchnumber, expressnumber, starttime, endtime);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("result", list);
		map.put("totalRows", rowCount);
		return map;
	}

	@Override
	public void batchInsertHeads(List<Tborderhead> orderheads) {
		tborderheadDao.batchInsert(orderheads);
	}

	@Override
	public void batchInsertBodies(List<Tborderlist> orderbodies) {
		tborderlistDao.batchInsert(orderbodies);
	}

	@Override
	public List<Tbreceipt> findReceipt(String receipttype, String expressnumber) {
		List<Tbreceipt> list = tbreceiptDao.findObject(receipttype, expressnumber);
		return list;
	}

	@Override
	public Tborderhead findOrderheadByOrderId(String orderid) {
		return tborderheadDao.findOrderheadByOrderId(orderid);
	}

	@Override
	public Map<String, Object> findAllOrderlistByOrderId(String orderid) {
		List<Tborderlist> list=tborderlistDao.findOrderlistByOrderId(orderid);
		int rowCount=tborderlistDao.findOrderlistRowCountByOrderId(orderid);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("result", list);
		map.put("totalRows", rowCount);
		return map;
	}

	@Override
	public List<Tborderlist> findOrderlistByOrderId(String orderid) {
		return tborderlistDao.findOrderlistByOrderId(orderid);
	}
	@Override
	public Result updateOrderHead(Tborderhead record) {
		try {
			orderheadMapper.updateByPrimaryKey(record);
			return Result.build(0, "修改订单orderhead信息成功");
		} catch (Exception ex) {
			ex.printStackTrace();
			return Result.build(1, "修改订单orderhead信息失败", ex.getMessage());
		}
	}

	@Override
	public Result updateOrderList(Tborderlist record) {
		try {
			orderlistMapper.updateByPrimaryKey(record);
			return Result.build(0, "修改订单orderhead信息成功");
		} catch (Exception ex) {
			ex.printStackTrace();
			return Result.build(1, "修改订单orderhead信息失败", ex.getMessage());
		}
	}

}
